#!/bin/bash 

# Date 		: 2021.1.25
# Author	: majin
# Mail 		: xx
# Function	: easy to compile for gdb in vscode 
# Version 	: 1.0
# Update 	: 2021.1.25

source ~/.extra_bashrc
name_msg;
now_dir=`pwd`

gcc_local="/root/install/gcc_install/bin/gcc-4.8-google-lipo/bin/gcc"

gcc_option="-g -O2 -v " 
#  -flto -fforce-lipo-majin=2  -fprofile-use=lipo-data --param lipo-dump-cgraph=1 -fripa --param force-lipo=1  -flto  -fprofile-generate -fprofile-use -fripa -lgcov 
# -frecord-compilation-info-in-elf

exe_name="mytest"
program_dir="/root/test/lipo/lipo"
program_name="a.c b.c c.c"

cd $program_dir

# cleaning all file created by compile
if [[ "clean" = $1 ]];then
    program_name=" $program_name test.data1 test.data2"
    info_msg "clean all file in $program_dir except $program_name"
    rmfile=`echo "$program_name" | awk 'gsub(/ /," ! -name ")'`
    # add some extra file for not rm
    find -mindepth 1 $rmfile |xargs rm -rf
    info_msg "success"
    cd $now_dir
    exit
fi

print-v(){
    debug_msg "$1 -v"
    $1 -v
    $1 -v > build.log 2>&1

    IFS=$'\n'
    for res in `grep "cc1 " build.log`
    do
        res=`eval echo $res`
        info_msg $res
        echo ""
        info_msg "parse ..."
        cc1=${res%%" "*}
        opt="${res#*"cc1 "} 2>&1"
        echo $cc1
        echo "\"$opt\""|awk 'gsub(/ /,"\",\"")'
        echo ""
        echo ""
        info_msg "success"
    done

    for res in `grep "lto1 " build.log`
    do
        res=`eval echo $res`
        info_msg $res
        echo ""
        info_msg "parse ..."
        lto1=${res%%" "*}
        opt="${res#*"lto1 "} 2>&1"
        echo $lto1
        echo "\"$opt\""|awk 'gsub(/ /,"\",\"")'
        echo ""
        echo ""
        info_msg "success"
    done
}
autofdo-gen(){
    debug_msg "$gcc_local -g -O2 -frecord-compilation-info-in-elf $program_name -o $exe_name "
    $gcc_local -g -O2 -frecord-compilation-info-in-elf $program_name -o $exe_name 

    debug_msg "perf record -b -e br_inst_retired.near_taken:P -o $exe_name.date ./$exe_name"
    perf record -b -e br_inst_retired.near_taken:P -o $exe_name.date ./$exe_name
    ls

    debug_msg "../autofdo/create_gcov --binary=$exe_name--profile=$exe_name.data --gcov=$exe_name.gcov -gcov_version=1"
    ../autofdo/create_gcov --binary=$exe_name--profile=$exe_name.data --gcov=$exe_name.gcov -gcov_version=1

    ls
}
# cleaning all file created by compile
if [[ "autofdo" = $1 ]];then
    ./$0 clean

    autofdo-gen

    debug_msg "$gcc_local -g -O2 -frecord-compilation-info-in-elf $program_name -o $exe_name "
    print-v "$gcc_local -g -O2 -frecord-compilation-info-in-elf -fauto-profile=$exe_name.gcov $program_name -o $exe_name"    

    ls
    cd $now_dir
    exit
fi

if [[ "autolipo" = $1 ]];then
    ./$0 clean

    autofdo-gen

    debug_msg "$gcc_local -O2 -frecord-compilation-info-in-elf -fauto-profile=$exe_name.gcov -fripa $program_name -o $exe_name "
    print-v "$gcc_local -O2 -frecord-compilation-info-in-elf -fauto-profile=$exe_name.gcov -fripa $program_name -o $exe_name" 
    
    ls
    cd $now_dir
    exit
fi

if [[ "lipo" = $1 ]];then
    ./$0 clean
    debug_msg "$gcc_local -O2 -fprofile-generate  -fripa $program_name -o $exe_name "
    $gcc_local -O2 -fprofile-generate  -fripa $program_name -o $exe_name 

    debug_msg "./$exe_name"
    ./$exe_name

    debug_msg "mv $exe_name $exe_name.lipo.gen"
    mv $exe_name $exe_name.lipo.gen

    ls

    debug_msg "$gcc_local -O2 -fprofile-use  -fripa $program_name -o $exe_name "
    print-v "$gcc_local -O2 -fprofile-use  -fripa $program_name -o $exe_name"    
    
    ls
    cd $now_dir
    exit
fi

print-v "$gcc_local $gcc_option $program_name -o $exe_name "

cd $now_dir